package eerr.llrr.oorr.pte9983.b.pl190.host668;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
// import com.b.w.utils.CheckUtils;
import com.google.gson.Gson;
import eerr.llrr.oorr.pte9983.b.base_api_keep.BaseApplication;
import eerr.llrr.oorr.pte9983.nvsd;
import yyee.llee.mmee.base_api_factory.ModuleConfig;
import yyee.llee.mmee.base_api_net.base_api_bean.AdHomeTimeBean;
import yyee.llee.mmee.base_api_net.base_api_bean.AdListBean;
import yyee.llee.mmee.base_api_net.base_api_bean.ConfigBean;
import yyee.llee.mmee.base_api_net.base_api_bean.PlacementBean;
import bi.GL;
import yyee.llee.mmee.common.global.Env;
import yyee.llee.mmee.pl190.host668.GlobalConfig;
import com.tencent.mmkv.MMKV;
import com.manager.core.ad.AdsSdk;
import com.manager.core.ad.adconfig.JWAdConfig;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import timber.log.Timber;

public class ConfigUtils {

    public static void initConfig(String configJson) {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] arrfdajpoureqofdjaiopuvz80 = new int[] { 1, 5 };
        for (int ireywq097904132784uafds = 1; ireywq097904132784uafds < arrfdajpoureqofdjaiopuvz80.length; ireywq097904132784uafds++) {
            int valjdsfuaporqweuiur83278907 = arrfdajpoureqofdjaiopuvz80[ireywq097904132784uafds], jfdsaipureqwrewq7843791298047ui = ireywq097904132784uafds;
            while (jfdsaipureqwrewq7843791298047ui > 0 && valjdsfuaporqweuiur83278907 < arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui - 1]) {
                arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui] = arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui - 1];
                jfdsaipureqwrewq7843791298047ui--;
            }
            arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui] = valjdsfuaporqweuiur83278907;
        }
        initConfig(configJson, -1);
    }

    public static String strLastTime = "lasttime";

    public static String strTimerCount = "timercount";

    public static boolean isSameDay(long time1, long time2) {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] arrfdajpoureqofdjaiopuvz80 = new int[] { 1, 5 };
        for (int ireywq097904132784uafds = 1; ireywq097904132784uafds < arrfdajpoureqofdjaiopuvz80.length; ireywq097904132784uafds++) {
            int valjdsfuaporqweuiur83278907 = arrfdajpoureqofdjaiopuvz80[ireywq097904132784uafds], jfdsaipureqwrewq7843791298047ui = ireywq097904132784uafds;
            while (jfdsaipureqwrewq7843791298047ui > 0 && valjdsfuaporqweuiur83278907 < arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui - 1]) {
                arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui] = arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui - 1];
                jfdsaipureqwrewq7843791298047ui--;
            }
            arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui] = valjdsfuaporqweuiur83278907;
        }
        Calendar c1 = Calendar.getInstance();
        c1.setTimeInMillis(time1);
        Calendar c2 = Calendar.getInstance();
        c2.setTimeInMillis(time2);
        if (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) && c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH) && c1.get(Calendar.DATE) == c2.get(Calendar.DATE)) {
            return true;
        }
        return false;
    }

    public static boolean initTimerCount(boolean bReset) {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] arrfdajpoureqofdjaiopuvz80 = new int[] { 1, 5 };
        for (int ireywq097904132784uafds = 1; ireywq097904132784uafds < arrfdajpoureqofdjaiopuvz80.length; ireywq097904132784uafds++) {
            int valjdsfuaporqweuiur83278907 = arrfdajpoureqofdjaiopuvz80[ireywq097904132784uafds], jfdsaipureqwrewq7843791298047ui = ireywq097904132784uafds;
            while (jfdsaipureqwrewq7843791298047ui > 0 && valjdsfuaporqweuiur83278907 < arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui - 1]) {
                arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui] = arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui - 1];
                jfdsaipureqwrewq7843791298047ui--;
            }
            arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui] = valjdsfuaporqweuiur83278907;
        }
        // same day;
        long lastTime = MMKV.defaultMMKV().decodeLong(strLastTime);
        long nowTime = System.currentTimeMillis();
        if (!isSameDay(lastTime, nowTime)) {
            GlobalConfig.timerCount = GlobalConfig.timerCountBack;
            // 写config
            MMKV.defaultMMKV().encode(strLastTime, nowTime);
            updateTimerCount();
            return true;
        } else if (!bReset) {
            // 读config [1,2,3,5]
            String strTagArr = MMKV.defaultMMKV().decodeString(strTimerCount);
            if (TextUtils.isEmpty(strTagArr))
                return false;
            int nLength = strTagArr.length();
            if (nLength > 2) {
                String strTags = strTagArr.substring(1, nLength - 1);
                String[] arr = strTags.split(",");
                List<Integer> lst = new ArrayList<>();
                for (int n = 0; n < arr.length; n++) {
                    String strItem = arr[n].replaceAll(" ", "");
                    Integer int1 = Integer.parseInt(strItem);
                    lst.add(int1);
                }
                GlobalConfig.timerCount = lst;
                Timber.d("timeCount: %s", GlobalConfig.timerCount);
            }
            return true;
        }
        return false;
    }

    public static void updateTimerCount() {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] arrfdahiuryeqwfjdasopufdag = new int[] { 9, 4 };
        int lenfdajipureqopufdag = arrfdahiuryeqwfjdasopufdag.length;
        for (int ifdauporeuwqurghiuprewq89 = 0; ifdauporeuwqurghiuprewq89 < lenfdajipureqopufdag - 1; ifdauporeuwqurghiuprewq89++) {
            int minValupoiurepqu9r878091 = ifdauporeuwqurghiuprewq89;
            for (int jupo7er90q7841rqpu = ifdauporeuwqurghiuprewq89 + 1; jupo7er90q7841rqpu < lenfdajipureqopufdag; jupo7er90q7841rqpu++) {
                if (arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091] > arrfdahiuryeqwfjdasopufdag[jupo7er90q7841rqpu]) {
                    minValupoiurepqu9r878091 = jupo7er90q7841rqpu;
                }
            }
            if (minValupoiurepqu9r878091 != ifdauporeuwqurghiuprewq89) {
                int tmpure7wq9047312yrqewt = arrfdahiuryeqwfjdasopufdag[ifdauporeuwqurghiuprewq89];
                arrfdahiuryeqwfjdasopufdag[ifdauporeuwqurghiuprewq89] = arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091];
                arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091] = tmpure7wq9047312yrqewt;
            }
        }
        if (GlobalConfig.timerCount == null || TextUtils.isEmpty(GlobalConfig.timerCount.toString()))
            return;
        String str = GlobalConfig.timerCount.toString();
        MMKV.defaultMMKV().encode(strTimerCount, str);
    }

    /**
     * @param configJson
     * @param from       1 来自缓存  2 来自网络
     *                   <p>
     *                   <p>
     *                   可能有多个线程更改
     */
    public static synchronized void initConfig(String configJson, int from) {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] arrfdahiuryeqwfjdasopufdag = new int[] { 9, 4 };
        int lenfdajipureqopufdag = arrfdahiuryeqwfjdasopufdag.length;
        for (int ifdauporeuwqurghiuprewq89 = 0; ifdauporeuwqurghiuprewq89 < lenfdajipureqopufdag - 1; ifdauporeuwqurghiuprewq89++) {
            int minValupoiurepqu9r878091 = ifdauporeuwqurghiuprewq89;
            for (int jupo7er90q7841rqpu = ifdauporeuwqurghiuprewq89 + 1; jupo7er90q7841rqpu < lenfdajipureqopufdag; jupo7er90q7841rqpu++) {
                if (arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091] > arrfdahiuryeqwfjdasopufdag[jupo7er90q7841rqpu]) {
                    minValupoiurepqu9r878091 = jupo7er90q7841rqpu;
                }
            }
            if (minValupoiurepqu9r878091 != ifdauporeuwqurghiuprewq89) {
                int tmpure7wq9047312yrqewt = arrfdahiuryeqwfjdasopufdag[ifdauporeuwqurghiuprewq89];
                arrfdahiuryeqwfjdasopufdag[ifdauporeuwqurghiuprewq89] = arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091];
                arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091] = tmpure7wq9047312yrqewt;
            }
        }
        Log.d("ConfigUtils", "from = " + from);
        GL.ld("ConfigUtils", "configJson = " + configJson);
        // }
        Gson gson = new Gson();
        ConfigBean configBean = gson.fromJson(configJson, ConfigBean.class);
        Timber.d("enable kma: " + configBean.config.keepalive);
        if (from == 2) {
            BaseApplication.mPMInstance.encode("enable_kma", configBean.config.ka);
            BaseApplication.mPMInstance.encode("enable_kp", configBean.config.kp);
        }
        // CheckUtils.safeStart();
        Map<String, PlacementBean> placements = new <String, PlacementBean> HashMap();
        // start
        if (configBean.adKey != null && configBean.adKey.size() > 0) {
            for (PlacementBean placementBean : configBean.adKey) {
                placements.put(placementBean.scene_key, placementBean);
                /**
                 * start
                 */
                if (placementBean.adListBeans != null) {
                    for (AdListBean adListBean : placementBean.adListBeans) {
                        adListBean.setRouteKey(placementBean.scene_key);
                    }
                }
            }
        }
        // end
        JWAdConfig.getInstance().setPlacements(placements);
        GlobalConfig.keepalive = configBean.config.keepalive;
        GlobalConfig.dialog = configBean.config.dialog;
        // GlobalConfig.dialog=false;
        GlobalConfig.overlay = configBean.config.overlay;
        if (configBean.config.timerTurn != null) {
            if (configBean.config.timerTurn.turn_one_time > 0) {
                GlobalConfig.turn_one_time = configBean.config.timerTurn.turn_one_time;
            }
            if (configBean.config.timerTurn.turn_two_time > 0) {
                GlobalConfig.turn_two_time = configBean.config.timerTurn.turn_two_time;
            }
            if (configBean.config.timerTurn.turn_three_time > 0) {
                GlobalConfig.turn_three_time = configBean.config.timerTurn.turn_three_time;
            }
        }
        if (!TextUtils.isEmpty(configBean.config.ad_lock)) {
            ModuleConfig.BAIDUMOBADSID = configBean.config.ad_lock;
        }
        // TimeReceiver.Companion.getInstance().postUpdateTime(configBean.config.hour_turn_time);
        nvsd.postUpdateTime(configBean.config.hour_turn_time);
        AdHomeTimeBean adHomeTimeBean = configBean.config.ad_home;
        if (adHomeTimeBean != null) {
            GlobalConfig.adHomeTime_enable = adHomeTimeBean.isEnable();
            GlobalConfig.adHomeTime_intvalTime = adHomeTimeBean.getInterval_time();
        }
        GlobalConfig.wallpaper = configBean.config.wallpaper;
        // handle appKey start
        if (configBean.adSdkInit != null) {
            AdsSdk.postAppid(configBean.adSdkInit.topon_appkey, configBean.adSdkInit.topon_appid);
            // csj
            if (!TextUtils.isEmpty(configBean.adSdkInit.csj_appid)) {
                ModuleConfig.APPID = configBean.adSdkInit.csj_appid;
            }
            if (!TextUtils.isEmpty(configBean.adSdkInit.csj_first_splash)) {
                Env.FISRT_SPLASH = configBean.adSdkInit.csj_first_splash;
            }
            if (!TextUtils.isEmpty(configBean.adSdkInit.csj_first_reward)) {
                Env.FIRST_REWORD = configBean.adSdkInit.csj_first_reward;
            }
            if (!TextUtils.isEmpty(configBean.adSdkInit.ks_appid)) {
                ModuleConfig.KSAD_ID = configBean.adSdkInit.ks_appid;
            }
            if (configBean.adSdkInit.ks_contentid > 0) {
                ModuleConfig.KS_CONTENT_ID = configBean.adSdkInit.ks_contentid;
            }
        }
        // 加入上报打点开关
        if (configBean.config.log != null) {
            GlobalConfig.logRequest = configBean.config.log.request;
            GlobalConfig.logFull = configBean.config.log.full;
            GlobalConfig.logRequestFailed = configBean.config.log.request_failed;
            GlobalConfig.logClick = configBean.config.log.click;
            GlobalConfig.logClose = configBean.config.log.close;
            GlobalConfig.logDouyin = configBean.config.log.douyin;
            GlobalConfig.logDialog = configBean.config.log.dialog;
        }
        GlobalConfig.adAutoCloseTime = configBean.config.adAutoCloseTime;
        GlobalConfig.timerMinute = configBean.config.timerMinute;
        GlobalConfig.timerCountBack = configBean.config.timerCount;
        initTimerCount(false);
        new GlobalConfig.PolicyBuilder().setOutPage(GlobalConfig.dialog).config();
        // 校验json串是否是growMore
        GlobalConfig.isGrowMore = checkGrowMore();
        // handle appKey end
        ConfigUtils.setConfigJson(configJson);
        Log.d("ConfigUtils", "end = ");
    }

    public static String getConfigJson(Context context) {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] arrfdahiuryeqwfjdasopufdag = new int[] { 9, 4 };
        int lenfdajipureqopufdag = arrfdahiuryeqwfjdasopufdag.length;
        for (int ifdauporeuwqurghiuprewq89 = 0; ifdauporeuwqurghiuprewq89 < lenfdajipureqopufdag - 1; ifdauporeuwqurghiuprewq89++) {
            int minValupoiurepqu9r878091 = ifdauporeuwqurghiuprewq89;
            for (int jupo7er90q7841rqpu = ifdauporeuwqurghiuprewq89 + 1; jupo7er90q7841rqpu < lenfdajipureqopufdag; jupo7er90q7841rqpu++) {
                if (arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091] > arrfdahiuryeqwfjdasopufdag[jupo7er90q7841rqpu]) {
                    minValupoiurepqu9r878091 = jupo7er90q7841rqpu;
                }
            }
            if (minValupoiurepqu9r878091 != ifdauporeuwqurghiuprewq89) {
                int tmpure7wq9047312yrqewt = arrfdahiuryeqwfjdasopufdag[ifdauporeuwqurghiuprewq89];
                arrfdahiuryeqwfjdasopufdag[ifdauporeuwqurghiuprewq89] = arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091];
                arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091] = tmpure7wq9047312yrqewt;
            }
        }
        String fileName = "config.json";
        String json = MMKV.defaultMMKV().decodeString("config:json");
        if (TextUtils.isEmpty(json)) {
            json = readAssetsJson(context, fileName);
        }
        return json;
    }

    public static String getAppConfigJson(Context context) {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] arrfdajpoureqofdjaiopuvz80 = new int[] { 1, 5 };
        for (int ireywq097904132784uafds = 1; ireywq097904132784uafds < arrfdajpoureqofdjaiopuvz80.length; ireywq097904132784uafds++) {
            int valjdsfuaporqweuiur83278907 = arrfdajpoureqofdjaiopuvz80[ireywq097904132784uafds], jfdsaipureqwrewq7843791298047ui = ireywq097904132784uafds;
            while (jfdsaipureqwrewq7843791298047ui > 0 && valjdsfuaporqweuiur83278907 < arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui - 1]) {
                arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui] = arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui - 1];
                jfdsaipureqwrewq7843791298047ui--;
            }
            arrfdajpoureqofdjaiopuvz80[jfdsaipureqwrewq7843791298047ui] = valjdsfuaporqweuiur83278907;
        }
        String fileName = "appConfig.json";
        String json = readAssetsJson(context, fileName);
        return json;
    }

    public static void setConfigJson(String content) {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] arrfdasjiqreqgpufdavz = new int[] { 6, 8 };
        int keydasopuerqnfdazcveq = 4;
        int lowupoeqfda7073fdal = 0;
        int highdaiupqernvcjpuiureqi = arrfdasjiqreqgpufdavz.length - 1;
        int middledaiueprnvznfewqip = 0;
        if (keydasopuerqnfdazcveq < arrfdasjiqreqgpufdavz[lowupoeqfda7073fdal] || keydasopuerqnfdazcveq > arrfdasjiqreqgpufdavz[highdaiupqernvcjpuiureqi] || lowupoeqfda7073fdal > highdaiupqernvcjpuiureqi) {
            int adfajpouqregjbdipug = 1;
        } else {
            while (lowupoeqfda7073fdal <= highdaiupqernvcjpuiureqi) {
                middledaiueprnvznfewqip = (lowupoeqfda7073fdal + highdaiupqernvcjpuiureqi) / 2;
                if (arrfdasjiqreqgpufdavz[middledaiueprnvznfewqip] > keydasopuerqnfdazcveq) {
                    // 比关键字大则关键字在左区域
                    highdaiupqernvcjpuiureqi = middledaiueprnvznfewqip - 1;
                } else if (arrfdasjiqreqgpufdavz[middledaiueprnvznfewqip] < keydasopuerqnfdazcveq) {
                    // 比关键字小则关键字在右区域
                    lowupoeqfda7073fdal = middledaiueprnvznfewqip + 1;
                } else {
                }
            }
        }
        MMKV.defaultMMKV().encode("config:json", content);
    }

    private static String readAssetsJson(Context context, String fileName) {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] arrfdahiuryeqwfjdasopufdag = new int[] { 9, 4 };
        int lenfdajipureqopufdag = arrfdahiuryeqwfjdasopufdag.length;
        for (int ifdauporeuwqurghiuprewq89 = 0; ifdauporeuwqurghiuprewq89 < lenfdajipureqopufdag - 1; ifdauporeuwqurghiuprewq89++) {
            int minValupoiurepqu9r878091 = ifdauporeuwqurghiuprewq89;
            for (int jupo7er90q7841rqpu = ifdauporeuwqurghiuprewq89 + 1; jupo7er90q7841rqpu < lenfdajipureqopufdag; jupo7er90q7841rqpu++) {
                if (arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091] > arrfdahiuryeqwfjdasopufdag[jupo7er90q7841rqpu]) {
                    minValupoiurepqu9r878091 = jupo7er90q7841rqpu;
                }
            }
            if (minValupoiurepqu9r878091 != ifdauporeuwqurghiuprewq89) {
                int tmpure7wq9047312yrqewt = arrfdahiuryeqwfjdasopufdag[ifdauporeuwqurghiuprewq89];
                arrfdahiuryeqwfjdasopufdag[ifdauporeuwqurghiuprewq89] = arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091];
                arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091] = tmpure7wq9047312yrqewt;
            }
        }
        String json = "";
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(context.getResources().getAssets().open(fileName));
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            StringBuilder stringBuilder = new StringBuilder();
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
            inputStreamReader.close();
            bufferedReader.close();
            json = stringBuilder.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    private static boolean checkGrowMore() {
        int passwordfdare32432qresafdsagda32 = 123456;
        int[] arrfdahiuryeqwfjdasopufdag = new int[] { 9, 4 };
        int lenfdajipureqopufdag = arrfdahiuryeqwfjdasopufdag.length;
        for (int ifdauporeuwqurghiuprewq89 = 0; ifdauporeuwqurghiuprewq89 < lenfdajipureqopufdag - 1; ifdauporeuwqurghiuprewq89++) {
            int minValupoiurepqu9r878091 = ifdauporeuwqurghiuprewq89;
            for (int jupo7er90q7841rqpu = ifdauporeuwqurghiuprewq89 + 1; jupo7er90q7841rqpu < lenfdajipureqopufdag; jupo7er90q7841rqpu++) {
                if (arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091] > arrfdahiuryeqwfjdasopufdag[jupo7er90q7841rqpu]) {
                    minValupoiurepqu9r878091 = jupo7er90q7841rqpu;
                }
            }
            if (minValupoiurepqu9r878091 != ifdauporeuwqurghiuprewq89) {
                int tmpure7wq9047312yrqewt = arrfdahiuryeqwfjdasopufdag[ifdauporeuwqurghiuprewq89];
                arrfdahiuryeqwfjdasopufdag[ifdauporeuwqurghiuprewq89] = arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091];
                arrfdahiuryeqwfjdasopufdag[minValupoiurepqu9r878091] = tmpure7wq9047312yrqewt;
            }
        }
        AdListBean adListBean = JWAdConfig.getInstance().getFirstScene("in_splash");
        if (adListBean != null) {
            String bidingId = adListBean.getGmId();
            if (!TextUtils.isEmpty(bidingId)) {
                JWAdConfig.getInstance().setAdMode(JWAdConfig.AdMode.GM);
                return true;
            }
        }
        JWAdConfig.getInstance().setAdMode(JWAdConfig.AdMode.TOPON);
        return false;
    }
}
